<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="jp.nyatla.ntg.psp.*" %>
<%@ page import="jp.nyatla.ntg.*" %>
<%@ page import="jp.nyatla.ntg.bts.*" %>
<%@ page import="twitter4j.*" %>
<%@ page import="java.util.*" %>

<%
	response.setHeader("pragma","no-cache");
	response.setHeader("Cache-Control","no-cache");
	
	TimelineTwitterSession s=new TimelineTwitterSession(request,true);
	TwitterSessionData sd=s.refSessionData();
	long g_now=s.now;

	//チェック
	if(!s.isSignIn()){
		%><jsp:forward page="error.view.jsp" /><%
	}
	//
	String g_msg=null;
	if(s.isError()){
		//通信エラー
	}else{
		//必要なデータを生成
	}

	//現在時刻
	s.commitSession();
%>
<html>
<head>
<style type="text/css">
	<% s.printCssFile(out);%>
</style>
<script type="text/javascript">

var pre_msg=<%=(g_msg==null?"null":"\""+g_msg+"\"")%>;

/////
//NyAPPFW
/////
function gElemById(i_id)
{
	return document.getElementById(i_id);
}

/////
function TlUi(i_id)
{
	this._id    =i_id;
	this.num    =this.tl.length;
	this.curidx =0;
	//初期化
	this._mdiv=gElemById(i_id+"r");
	this.setCursorState(0,true);
	for(var i=this.num-1;i>0;i--){
		this.setCursorState(i,false);
	}	
	this._tid=null;
	var _t=this;
	this.timerProc=function()
	{
		if(_t._epos==_t._cpos){
			clearInterval(_t._tid);
			_t._tid=null;
			return;
		}
		_t._cpos=(_t._epos+_t._cpos*_t._apm.m)/_t._apm.d;
		if(Math.abs(_t._cpos-_t._epos)<_t._apm.l){
			_t._cpos=_t._epos;
		}
		window.scrollTo(0,_t._cpos);
	}	
}




TlUi.prototype=
{
	tl:<% s.printTlJS(out);%>,
	cs:[{f:"#99ffff",b:"#e0e0e0"},{f:"#FFFF99",b:"#FFFFe0"},{f:"#99FFCC",b:"#e0FFe0"}],
	_apm:<%s.printUiSetting(out);%>,
	curidx:0,
	num:0,
	_id:null,
	_epos:0,	//目的の位置
	_cpos:0,	//現在の位置
	_mdiv:null,
	timerProc:null,
	_tid:null,
	getSelectedTl:function()
	{
		return this.tl[this.curidx];
	},
	setCursorState:function(i_idx,i_act)
	{
		var s=gElemById(this._id+i_idx).style;
		var tli=this.tl[i_idx];
		var cid=tli.hm?1:tli.rt?2:0;
		if(i_act){
			s.backgroundColor=this.cs[cid].f;
		}else{
			s.backgroundColor=this.cs[cid].b;
		}
	},
	setCursorPos:function(i_idx,is_animation)
	{
		var _VIEW_HEIGHT=window.innerHeight;
		if(i_idx<0 || i_idx>=this.num){
			return false;
		}
		this.setCursorState(this.curidx,false);
		this.curidx=i_idx;
		this.setCursorState(i_idx,true);

		//divの移動位置を決定
		var el=gElemById("s"+this.curidx);
		var ep=(el.offsetTop+el.offsetHeight/2-_VIEW_HEIGHT/2);
		if(ep<0){
			ep=0;
		}
		else if((this._TL_HEIGHT*this.num)-ep<_VIEW_HEIGHT)
		{
			ep=(this._TL_HEIGHT*this.num)-_VIEW_HEIGHT;
		}
		this._epos=ep;
		if(is_animation){
			this.startAnimation();
		}else{
			this._cpos=ep;
			window.scrollTo(0,this._cpos);
		}
		return true;
	},
	//カーソルをsid以前のTLにセットする。
	setCursorBySid:function(i_sid)
	{
		for(var i=0;i<this.num;i++){
			if(this.tl[i].id<=i_sid){
				this.setCursorPos(i,false);
				return true;
			}
		}
		return false;
	},
	next:function()
	{
		return this.setCursorPos(this.curidx+1,true);
	},
	prev:function()
	{
		return this.setCursorPos(this.curidx-1,true);
	},
	startAnimation:function()
	{
		if(this._epos==this._cpos)
		{
			return;
		}
		if(this._tid!=null){
			return;
		}
		this._tid=setInterval(this.timerProc,this._apm.i);
	},
	stopAnimation:function()
	{
		if(this._tid!=null){
			clearInterval(this._tid);
		}
		this._tid=null;
	},
	getCurrentSid:function()
	{
		return this.tl[this.curidx].id;
	}
}
</script>
</head>

<body onload="_onload();" onunload="_onunload();">
<div id="sr" class="sp">
	<% s.printTlDiv(out);%>
</div>

<script type="text/javascript">
//グローバル変数
var app=null;
var ui=null;

function _onload()
{
	if(window.parent._nyatla_jp==undefined)
	{
		alert("no app root");
	}
	app=window.parent._nyatla_jp.getAppInst("ptg");
	ui=new TlUi("s");
	if(pre_msg!=null){
		app.ms_ui.pushMsg(pre_msg);
	}
	app.cbFrameLoad(app.UIID.TL,window,null);
	gElemById("sr").style.visibility="visible";
}
function _onunload()
{
	window.onfocus=null;
	if(ui!=null){
		ui.stopAnimation();
	}
	app.cbFrameUnload(app.UIID.TL);
}
</script>

</body>
</html>